package com.yidu.DatelessManagement.mapper;

import com.yidu.BusinessParament.entity.Cash;
import com.yidu.DatelessManagement.entity.NetWorth;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 净值统计数据访问层
 * @author 任俊杰
 * @version 1.0
 * @date 2022/12/27
 */

@Mapper
public interface NetWorthMapper {

    /**
     * 查询净值统计表所有信息
     * @param map 数据映射
     * @return List<NetWorth> 净值统计集合
     */
    @Select("select * from netWorth where fundId =#{fundId} ${sqlWhere} order by sortId")
    List<NetWorth> selectAllNetWorthNoLimit(Map<String, Object> map);

    /**
     * 获取总行数
     * @param map 集合数据映射
     * @return int 返回一个int类型的参数
     */
    @Select("select count(*) from netWorth where fundId = #{fundId} and createTime like #{createTime}")
    int getSumRow(Map<String, Object> map);


    /**
     * 清空当日的统计数据
     * @param map 集合数据映射
     * @return int 返回一个int类型的参数
     */
    @Delete("delete from netWorth where fundId = #{fundId} and createTime like #{createTime}")
    int deleteSameDayStatisticalData(Map<String, Object> map);


    /**
     * 获取证券信息
     * @param map map集合
     * @return List<NetWorth> 净值统计对象集合
     */
    List<NetWorth> getSecuritiesMsg(Map<String, Object> map);


    /**
     * 新增父模块数据
     * @param netWorth 净值统计对象
     * @return int 返回一个int类型的参数
     */
    @Insert("insert into netWorth(projectName, parentProjectName, fundId, createTime, sortId) VALUES (#{projectName}, #{parentProjectName}, #{fundId}, #{createTime}, #{sortId})")
    int insertParentModule(NetWorth netWorth);


    /**
     * 新增子模块数据
     * @param netWorth 净值统计对象
     * @return int 返回一个int类型的参数
     */
    @Insert("insert into netWorth(projectName, parentProjectName, fundId, projectId, shareNum, stopPrice, capital, marketValue, estimate, createTime) values (#{projectName}, #{parentProjectName}, #{fundId}, #{projectId}, #{shareNum}, #{stopPrice}, #{capital}, #{marketValue}, #{estimate}, #{createTime})")
    int insertSecurities(NetWorth netWorth);


    /**
     * 获取现金对象集合(现金账户表) 查询现金账户的全部 查现金库存的现金余额
     * @param map map集合
     * @return List<Cash>现金对象集合
     */
    @Select("SELECT c.*, cs.`money` FROM cash c JOIN cashStock cs ON c.cashId = cs.cashId WHERE 1 = 1 AND cs.fundId = #{fundId} AND cs.date =#{date}")
    List<Cash> getCash(Map<String, Object> map);


    /**
     * 获取现金子模块（账户）下的存款利息(现金账户表)
     * businessType是现金应收应付库存里的业务类型 标识 备注：1为清算款，2为估值增值，3为债券利息，4为申购赎回
     * fundId是现金库存表里的fundId
     * 它是查现金表 join 现金库存表 join 现金应收应付表 date是现金库存的
     * @param map 数据映射集合
     * @return List<Cash> 现金对象集合
     */
    @Select("SELECT c.*, cs.money, cs.fundId, cas.money FROM cash c JOIN cashStock cs ON c.cashId = cs.cashId JOIN cashAccrualStock cas ON cas.cashId = c.cashId WHERE 1 = 1 AND cas.businessType =#{businessType} AND fundId =#{fundId} AND cas.date =#{date} ")
    List<Cash> getCashInterestOnDeposit(Map<String, Object> map);


    /**
     * 获取证券清算款 证券应收应付表(库存)
     * date是证券应收应付库存的 这里查询的是TA库存表
     * @param map 数据映射集合
     * @return 返回一个int类型的参数
     */
    @Select("select sum(money * businessState) capital from securityAccrualStock where fundId = #{fundId} and date = #{date}")
    int getSecuritiesLiquidationMoney(Map<String, Object> map);


    /**
     * 获取股票单位净值的集合(证券库存表)----- JOIN security 证券表  securityId是证券表的证券id  securityType是证券表里的类型  fundId是证券库存表里的fundId date是证券库存的
     * @param map 数据映射
     * @return List<Double> 单位净值集合
     */
    @Select("SELECT unitCost FROM securityStock ss JOIN SECURITY s ON ss.securityId = s.securityId WHERE ss.`fundId`=#{fundId}  AND ss.`date`=#{date} AND s.`securityType` = 1")
    List<Double> getUnitNetWorth(Map<String, Object> map);

}
